//
//  WBComposeTypeVie.swift
//  Weibo
//
//  Created by kobe on 2016/12/29.
//  Copyright © 2016年 kobe. All rights reserved.
//

import UIKit

class WBComposeTypeView: UIView {
    
    @IBOutlet weak var scrollView: UIScrollView!
    
    /// 关闭按钮约束
    @IBOutlet weak var closeBtnCenterX: NSLayoutConstraint!
    /// 返回前一页约束
    @IBOutlet weak var returnBtnCenterX: NSLayoutConstraint!
    /// 返回按钮
    @IBOutlet weak var returnButton: UIButton!
    
    
    fileprivate let buttonInfo = [["imageName":"tabbar_compose_idea","title":"文字"],
        ["imageName":"tabbar_compose_photo","title":"照片/视频"],
        ["imageName":"tabbar_compose_weibo","title":"长微博"],
        ["imageName":"tabbar_compose_lbs","title":"签到"],
        ["imageName":"tabbar_compose_review","title":"点评"],
        ["imageName":"tabbar_compose_more","title":"更多","actionName":"clickMore"],
        ["imageName":"tabbar_compose_friend","title":"好友圈"],
        ["imageName":"tabbar_compose_wbcamera","title":"微博相机"],
        ["imageName":"tabbar_compose_music","title":"音乐"],
        ["imageName":"tabbar_compose_shooting","title":"拍摄"]]

    class func composeTypeView() -> WBComposeTypeView {
        let nib = UINib(nibName: "WBComposeTypeView", bundle: nil)
        
        // 从xib加载完成 就会调用awakeFromNib
        let v = nib.instantiate(withOwner: nil, options: nil)[0] as! WBComposeTypeView
        
        v.frame = UIScreen.main.bounds
        
        v.setUpUI()
        
        return v
    }
    
    /// 显示当前视图
    func show() {
        // 讲当前视图添加到 根视图控制器的 view
        guard let vc = UIApplication.shared.keyWindow?.rootViewController else {
            return
        }
        
        // 添加视图
        vc.view.addSubview(self)
        
    }
    
    //MARK: - 监听方法
    @objc fileprivate func clickButton() {
        print("click")
    }
    
    /// 点击更多
    @objc fileprivate func clickMore() {
        print("点击更多")
        
        scrollView.setContentOffset(CGPoint(x: scrollView.bounds.width, y: 0), animated: true)
        
        // 处理底部按钮
        returnButton.isHidden = false
        
        let margin = scrollView.bounds.width / 6
        closeBtnCenterX.constant += margin
        returnBtnCenterX.constant -= margin
        
        UIView.animate(withDuration: 0.25) { 
            self.layoutIfNeeded()
        }
        
    }
    
    
    @IBAction func back() {
        scrollView.setContentOffset(CGPoint(x: 0, y: 0), animated: true)
        
//        let margin = scrollView.bounds.width / 6
        closeBtnCenterX.constant = 0
        returnBtnCenterX.constant = 0
        UIView.animate(withDuration: 0.25, animations: { 
            self.layoutIfNeeded()
            self.returnButton.alpha = 0
            }) { (_) in
                self.returnButton.isHidden = true
                self.returnButton.alpha = 1
        }
        
    }
    
    @IBAction func close() {
        removeFromSuperview()
    }
    
}

fileprivate extension WBComposeTypeView {
    func setUpUI() {
        
        // 强行更新布局
        layoutIfNeeded()
        
        
        // 向scrollview添加视图
        let rect = scrollView.bounds
        let width = scrollView.bounds.width
        
        for i in 0..<2 {
            let v = UIView(frame: rect.offsetBy(dx: CGFloat(i) * width, dy: 0))
            
            addButtons(v: v, idx: i * 6)
            // 讲视图添加到scrollView
            scrollView.addSubview(v)

        }
        
        
        scrollView.contentSize = CGSize(width: 2 * width, height: 0)
        scrollView.showsVerticalScrollIndicator = false
        scrollView.showsHorizontalScrollIndicator = false
        scrollView.bounces = false
        scrollView.isScrollEnabled = false
        scrollView.isPagingEnabled = true
        
    }
    
    func addButtons(v:UIView,idx:Int)  {
        
        // 从 idx 开始添加6个
        let count = 6
        for i in idx..<(idx + count) {
            
            if i >= buttonInfo.count {
                break
            }
            
            // 从数组中获取图像名称和title
            let dict = buttonInfo[i]
            
            // 创建按钮
            guard let imageName = dict["imageName"],
                let title = dict["title"] else {
                    continue
            }
            
            let btn = WBComposeTypeButton.composeTypeButton(imageName: imageName, title: title)
            
            v.addSubview(btn)
            
            if let actionName = dict["actionName"] {
                btn.addTarget(self, action: Selector(actionName), for: .touchUpInside)
            }
            
        }
        
        // 布局按钮
        let btnSize = CGSize(width: 100, height: 100)
        let margin = (v.bounds.width - 3 * btnSize.width) / 4
        
        
        for (i,btn) in v.subviews.enumerated() {
            let y: CGFloat = (i > 2) ? (v.bounds.height - btnSize.height) : 0
            let col = i % 3
            let x = CGFloat(col + 1) * margin + CGFloat(col) * btnSize.width
            
            btn.frame = CGRect(x: x, y: y, width: btnSize.width, height: btnSize.height)
            
            
        }
        
    }
    
    
}
